<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        class Subject {
            constructor() {
                this.observers = []
            }
            // 添加观察者方法
            add(observer) {
                this.observers.push(observer)
            }
            // 观察者通知方法
            notify() {
                this.observers.forEach(item => {
                    item.update()
                })
            }
            remove(observer) {
                // splice    ---- 方法1
                // filter    ---- 方法2
                this.observers = this.observers.filter(item => item !== observer)

            }
        }
        class Observer {
            constructor(name) {
                this.name = name
            }
            update() {
                console.log("update", this.name);
            }
        }
        const subJect = new Subject()
        const observer1 = new Observer('jing')
        const observer2 = new Observer('井')
        subJect.add(observer1)
        subJect.add(observer2)
        setTimeout(() => {
            subJect.remove(observer1) //移除观察者模式
        }, 1000);
        setTimeout(() => {
            subJect.notify()
        }, 1500);
    </script>
</body>

</html>